package com.adams.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.adams.access.db.bean.DicColumn;
import com.adams.action.view.ViewAction;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.core.dto.DicColumnDto;
import com.adams.custom.page.util.ViewUtil;
import com.adams.utils.DateUtil;

/**
 * 报表ACTION
 * 
 * @author 904912 <br>
 *         2011-09-08
 * 
 */
public class ReportsAction extends DcxtBaseAction {

    public String getExcelData() throws BusinessException {
        List<Map> countList = dayGridDataBusinessMoney(false);
        super.getExcelData(countList);
        return null;
    }

    /**
     * 每天营业额(虚拟表展示数据)
     * <p>
     * 1. 字段1 时间 （年月日） 字段2：总收入(当天实收金额总和) 字段3 主键 字段4 卖出菜份数
     * </p>
     * 
     * @return
     * @throws BusinessException
     */
    public String dayGridDataBusinessMoney() throws BusinessException {
        List<Map> countList = null;
        countList = dayGridDataBusinessMoney(false);
        Map map = viewParseService.getParseGridDatasFromList(getUserId(), gridPanelId, countList, countList.size());
        write(JSONObject.fromObject(map).toString());
        return null;
    }

    private List<Map> dayGridDataBusinessMoney(boolean flag) throws BusinessException {

        Map inputMap = getInputMap();
        List<Map> countList = null;
        String startTimeStr = (String) inputMap.get("date_before_v_date");
        String endTimeStr = (String) inputMap.get("date_after_v_date");
        StringBuilder sql = new StringBuilder(
                "select  left(order_time,10) , sum(count) ,sum(rate_money) from  dcxt_order  o  ,dcxt_order_dishes  d  where  order_status='1' and   o.id = d.order_id ");
        if (StringUtils.isNotBlank(startTimeStr) && StringUtils.isNotBlank(endTimeStr)) {
            sql.append(" and  order_time >='" + startTimeStr + " 00:00:00' and  order_time < '" + endTimeStr
                    + " 23:59:59' ");
        } else if (StringUtils.isBlank(startTimeStr) && StringUtils.isNotBlank(endTimeStr)) {
            sql.append(" and  order_time <='" + endTimeStr + " 23:59:59' ");
        } else if (StringUtils.isNotBlank(startTimeStr) && StringUtils.isBlank(endTimeStr)) {
            sql.append(" and  order_time >='" + startTimeStr + " 00:00:00' ");
        } else {
            String date = DateUtil.dateToString(new Date(), "yyyy-MM-dd");
            sql.append(" and  order_time >='" + date + " 00:00:00' and  order_time < '" + date + " 23:59:59' ");
        }

        sql.append("   group by left(order_time,10) ");
        String[] columnNames = { "时间", "菜份数", "总金额" };
        countList = getCountList("v_dcxt_dayMoney", columnNames, sql.toString(), flag, inputMap);
        return countList;
    }

    /**
     * countFlag = true :返回全部 =false 返回limit条数
     * 
     * @throws BusinessException
     */
    private List<Map> getCountList(String tableCode, String[] columnNames, String sql, boolean countFlag, Map inputMap)
            throws BusinessException {
        List<Map> countList = null;
        String start = (String) inputMap.get("start");
        String limit = (String) inputMap.get("limit");
        List<DicColumn> dicColumns = dicTableService.findDBDicColumnsByTablecode(tableCode);
        List<DicColumnDto> dicColumnDtos = new ArrayList<DicColumnDto>();
        for (DicColumn dicColumn : dicColumns) {
            dicColumn2DicColumnDtoList(dicColumn, dicColumnDtos, columnNames);
        }
        if (countFlag) {
            countList = dicTableService.findData(tableCode, dicColumnDtos, sql.toString(), 0, 0);
        } else {
            // 导出全部
            countList = dicTableService.findData(tableCode, dicColumnDtos, sql.toString(), Integer.parseInt(start),
                    Integer.parseInt(limit));
        }

        return countList;
    }

    // -------------------------------END---------------------------------------------
    private void dicColumn2DicColumnDtoList(DicColumn dicColumn, List<DicColumnDto> dicColumnDtos, String columnNames[]) {
        if (dicColumnDtos == null) {
            dicColumnDtos = new ArrayList<DicColumnDto>();
        }
        if (columnNames != null) {
            for (String colName : columnNames) {
                if (dicColumn.getColumnname().equals(colName)) {
                    DicColumnDto dicColumnDto = new DicColumnDto("", dicColumn);
                    dicColumnDtos.add(dicColumnDto);
                }

            }
        }
    }

    private Map getInputMap() {
        if (ViewUtil.isNuLLStr(gridPanelId)) {
            gridPanelId = request.getParameter("gridPanelId");
        }
        if (ViewUtil.isNuLLStr(searchPanelId)) {
            searchPanelId = request.getParameter("searchPanelId");
        }
        Map inputMap = ViewAction.requestParameterMap2Map(request);
        return inputMap;
    }
}
